package com.weixx.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.weixx.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 测试前后端分离传递数据
 * @Auther weixx
 * @Date 2022/1/11
 */
@Controller
public class UserController {

    //加上这个注解就不会走试图解析器，直接返回字符串
    @RequestMapping(value = "/j1",method = RequestMethod.GET)
    @ResponseBody
    public String json1() throws JsonProcessingException {
    User user=new User(1,"weixx",24);
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(user);
    }


    @RequestMapping("/j2")
    @ResponseBody
    public String json12() throws JsonProcessingException {
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(sdf.format(date));
    }
}
